﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using SignDetection;

namespace Plate
{
    public partial class CarImageForm : Form
    {
        Bitmap ori_map;
        public CarImageForm()
        {
            InitializeComponent();
        }

        private void CarImageForm_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“crowdDataSet.CarImages”中。您可以根据需要移动或移除它。
            this.carImagesTableAdapter.Fill(this.crowdDataSet.CarImages);
            // TODO: 这行代码将数据加载到表“crowdDataSet.CarImages”中。您可以根据需要移动或移除它。
            System.IO.FileInfo[] files = null;
            try
            {
                System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("..\\plate\\可分割字符图片");
                files = dir.GetFiles("*.jpg");
            }
            catch(Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
                return;
            }
            //for (int i = 0; i < files.Length; i++)
            //{
            //    ListViewItem item = this.listViewImage.Items.Add(files[i].FullName);
            //    item.Tag = files[i];
            //}
        }

        private void dataGridViewImages_DoubleClick(object sender, EventArgs e)
        {
            if (this.dataGridViewImages.SelectedRows.Count == 0)
                return;
            string file = (string)this.dataGridViewImages.SelectedRows[0].Cells[0].Value;

            SignDetectionForm form = new SignDetectionForm(file.Trim());
            form.MdiParent = this.MdiParent;

            form.Show();

        }

        private void toolStripButtonImportImages_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "图像文件|*.jpg";
            dlg.Multiselect = true;
            if (dlg.ShowDialog() != DialogResult.OK)
                return;
            CoCarImages co = new CoCarImages();
            try
            {
                for (int i = 0; i < dlg.FileNames.Length; i++)
                {
                    co.InsertFile(dlg.FileNames[i]);
                }
            }
            catch (Exception exp)
            {
                MessageBox.Show(this, exp.ToString());
            }
        }


        private void dataGridViewImages_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void toolStripButtonTestAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.dataGridViewImages.RowCount-1; i++)
            {
                string filename = ((string)this.dataGridViewImages[0, i].Value).Trim();
                Test(filename);
                if (!(this.dataGridViewImages[1, i].Value is DBNull))
                {
                    string carnumber = ((string)this.dataGridViewImages[1, i].Value).Trim();
//                    this.dataGridViewImages[4, i].Value=TestCarNumber(filename, carnumber);
//                    this.dataGridViewImages[4, i].Value = true;
                    TestCarNumber(filename,carnumber);
                    this.dataGridViewImages[4, i].Value = Result;
                    
                }
            }
        }

        /// <summary>
        /// 测试车牌识别
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="storedCarnumber"></param>
        List<Bitmap> Segment()
        {
            SignDetection.SignDetector detector = SignDetection.SignDetector.CreateSignDetector();
            //List<Bitmap> signs = detector.SegmentSigns(ori_Bitmap);
            List<Bitmap> signs = detector.DetectSign(ori_map);

                //if (signs.Count >= 1)
                //    this.character1.Image = signs[0];
                //if (signs.Count >= 2)
                //    this.character2.Image = signs[1];
                //if (signs.Count >= 3)
                //    this.character3.Image = signs[2];
                //if (signs.Count >= 4)
                //    this.character4.Image = signs[3];
                //if (signs.Count >= 5)
                //    this.character5.Image = signs[4];
                //if (signs.Count >= 6)
                //    this.character6.Image = signs[5];
                //if (signs.Count >= 7)
                //    this.character7.Image = signs[6];

                return signs;
               
        }
        Boolean Result;
        void TestCarNumber(string filename, string storedCarnumber)
        {
            ori_map = new Bitmap(filename);
            bool mark1;
            int result;
            try
            {
                GaborRec.GaborRecognize recog = new GaborRec.GaborRecognize();

                List<Bitmap> signs = this.Segment();
                string number = "";
                //保存测试样本
                for (int i = 0; i < signs.Count; i++)
                {
                    string path = Application.StartupPath + "\\" + i + ".bmp";
                    signs[i].Save(path);
                }
                for (int i = 0; i < signs.Count; i++)
                {
                    char c = recog.Recognize(signs[i]);
                    number += c;
                }
                result = String.Compare(number, storedCarnumber);
                if (result == 0)
                    mark1 = true;
                else
                    mark1 = false;
                CoCarImages coimg = new CoCarImages();
                coimg.SafaNumberResult(filename, mark1);
                Result = mark1;
       
            }
            catch (System.Exception exp)
            {
                MessageBox.Show(this, exp.ToString());

            }
        }


        //测试车牌定位
        void Test(string filename)
        {
            Bitmap bitmap = new Bitmap(filename);
            //SignDetection.
            bool mark = false;
            SignDetection.SignDetector detector = SignDetection.SignDetector.CreateSignDetector();
            //detector.PlatePositioning(bitmap,ref mark);
            CoCarImages co = new CoCarImages();
            co.SafaTestResult(filename, mark);
        }

        private void ToolStripMenuItemPlatePositioning_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewImages.SelectedRows.Count == 0)
                return;
            string file = (string)this.dataGridViewImages.SelectedRows[0].Cells[0].Value;
            SignDetectionForm form = new SignDetectionForm(file.Trim());
            form.MdiParent = this.MdiParent;

            form.Show();
        }

        private void ToolStripMenuItemPlateRecognizing_Click(object sender, EventArgs e)
        {
            if (this.dataGridViewImages.SelectedRows.Count == 0)
                return;
            string file = (string)this.dataGridViewImages.SelectedRows[0].Cells[0].Value;
            RecogForm form = new RecogForm(file.Trim());
            form.MdiParent = this.MdiParent;

            form.Show();
        }

        private void dataGridViewImages_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void ToolStripMenuItemAddColor_Click(object sender, EventArgs e)
        {

            SignDetection.ColorClassiffierForm form = new SignDetection.ColorClassiffierForm();
            form.MdiParent = this.MdiParent;
            form.Show();
        }
    }
}
